<template>
  <el-main>
    <el-card shadow="never" style="width: 1500px">
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="活动介绍">
          <el-input
            v-model="form.intro"
            type="textarea"
            :rows="10"
            style="width: 800px"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">立即保存</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card
      shadow="never"
      style="width: 1500px; margin-top: 20px"
      class="daily_task"
    >
      <el-button type="primary" icon="el-icon-plus" @click="addTask"
        >添加消费金额</el-button
      >
      <el-empty
        description="暂无消费金额"
        v-if="timeLine.length == 0"
      ></el-empty>
      <el-timeline style="margin-top: 20px" v-else>
        <el-timeline-item
          :timestamp="`消费满${item.spend_amount}元可领取`"
          placement="top"
          v-for="(item, index) in timeLine"
          :key="item.id"
        >
          <el-card>
            <el-button
              type="primary"
              icon="el-icon-plus"
              @click="addReword(index)"
              >添加奖品</el-button
            >
            <el-button
              type="danger"
              icon="el-icon-delete"
              @click="delReword(index)"
            ></el-button>
            <el-table
              border
              :data="item.give_content"
              style="width: 100%; margin-top: 20px"
            >
              <el-table-column label="类型">
                <template #default="scope2">
                  <span v-if="scope2.row.type == 'balance'">余额</span>
                  <span v-if="scope2.row.type == 'coupon'">优惠券</span>
                  <span v-if="scope2.row.type == 'blindbox'">盲盒</span>
                  <span v-if="scope2.row.type == 'prop'">道具卡</span>
                </template>
              </el-table-column>
              <el-table-column prop="name" label="名称"> </el-table-column>
              <el-table-column label="数量">
                <template #default="scope2">
                  <span v-if="scope2.row.type == 'balance'">{{
                    scope2.row.value
                  }}</span>
                  <span v-else>{{ scope2.row.num }}</span>
                </template>
              </el-table-column>
              <el-table-column label="操作">
                <template #default="scope">
                  <el-button
                    @click="handleDel(index, scope.$index)"
                    type="text"
                    size="small"
                    >删除</el-button
                  >
                </template>
              </el-table-column>
            </el-table>
          </el-card>
        </el-timeline-item>
        <el-button type="primary" @click="saveTask" style="margin-top: 20px"
          >保存任务</el-button
        >
      </el-timeline>
    </el-card>
    <el-dialog
      title="添加消费金额"
      v-model="dialogVisible"
      width="300px"
      :before-close="handleClose"
    >
      <el-input v-model="spendAmount" type="number"></el-input>
      <el-button type="primary" @click="addAmount" style="margin-top: 20px"
        >确 定</el-button
      >
    </el-dialog>

    <el-dialog
      title="选择资源"
      v-model="resourceVisible"
      :width="1200"
      destroy-on-close
      :close-on-click-modal="false"
    >
      <resource @selectedResource="handleResource"></resource>
    </el-dialog>
  </el-main>
</template>
<script>
import resource from "@/components/resource";
export default {
  components: {
    resource,
  },
  data() {
    return {
      timeLine: [],
      form: {
        type: "intro",
        intro: "",
      },
      resourceVisible: false,
      spendAmount: 0,
      nowIndex: 0,
      dialogVisible: false,
    };
  },
  mounted() {
    this.getInfo();
  },
  methods: {
    async getInfo() {
      let res = await this.$API.dailyTask.getSetInfo.get();
      this.form.intro = res.data.intro.daily_task_intro;
      this.timeLine = res.data.task;
    },
    // 保存介绍
    async onSubmit() {
      let res = await this.$API.dailyTask.set.post(this.form);
      if (res.code == 0) {
        this.$message.success(res.msg);
      } else {
        this.$message.error(res.msg);
      }
    },
    // 删除
    handleDel(index, tableIndex) {
      this.timeLine[index].give_content.splice(tableIndex, 1);
    },
    // 添加金额
    addTask() {
      this.dialogVisible = true;
    },
    // 添加金额
    addAmount() {
      this.timeLine.push({
        spend_amount: this.spendAmount,
        give_content: [],
      });
      this.dialogVisible = false;
    },
    // 添加奖品
    addReword(index) {
      this.nowIndex = index;
      this.resourceVisible = true;
    },
    // 保存任务
    async saveTask() {
      let res = await this.$API.dailyTask.saveTask.post({
        timeLine: this.timeLine,
      });
      if (res.code == 0) {
        this.$message.success("保存成功");
      } else {
        this.$message.error(res.msg);
      }
    },
    // 选择了资源
    handleResource(row) {
      this.resourceVisible = false;
      this.timeLine[this.nowIndex].give_content.push(row);
    },
    // 删除奖励
    delReword(index) {
      this.timeLine.splice(index, 1);
    },
  },
};
</script>
<style>
.daily_task .el-timeline-item__timestamp {
  color: #000;
}
.daily_task .el-timeline-item__content {
  width: 800px !important;
}
</style>
